package cn.iocoder.yudao.module.member.controller.admin.usercreativeremark;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.usercreativeremark.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.usercreativeremark.UserCreativeRemarkDO;
import cn.iocoder.yudao.module.member.service.usercreativeremark.UserCreativeRemarkService;

@Tag(name = "管理后台 - 创意文案关联评论")
@RestController
@RequestMapping("/member/user-creative-remark")
@Validated
public class UserCreativeRemarkController {

    @Resource
    private UserCreativeRemarkService userCreativeRemarkService;

    @PostMapping("/create")
    @Operation(summary = "创建创意文案关联评论")
    @PreAuthorize("@ss.hasPermission('member:user-creative-remark:create')")
    public CommonResult<Long> createUserCreativeRemark(@Valid @RequestBody UserCreativeRemarkSaveReqVO createReqVO) {
        return success(userCreativeRemarkService.createUserCreativeRemark(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新创意文案关联评论")
    @PreAuthorize("@ss.hasPermission('member:user-creative-remark:update')")
    public CommonResult<Boolean> updateUserCreativeRemark(@Valid @RequestBody UserCreativeRemarkSaveReqVO updateReqVO) {
        userCreativeRemarkService.updateUserCreativeRemark(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除创意文案关联评论")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-creative-remark:delete')")
    public CommonResult<Boolean> deleteUserCreativeRemark(@RequestParam("id") Long id) {
        userCreativeRemarkService.deleteUserCreativeRemark(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得创意文案关联评论")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-creative-remark:query')")
    public CommonResult<UserCreativeRemarkRespVO> getUserCreativeRemark(@RequestParam("id") Long id) {
        UserCreativeRemarkDO userCreativeRemark = userCreativeRemarkService.getUserCreativeRemark(id);
        return success(BeanUtils.toBean(userCreativeRemark, UserCreativeRemarkRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得创意文案关联评论分页")
    @PreAuthorize("@ss.hasPermission('member:user-creative-remark:query')")
    public CommonResult<PageResult<UserCreativeRemarkRespVO>> getUserCreativeRemarkPage(@Valid UserCreativeRemarkPageReqVO pageReqVO) {
        PageResult<UserCreativeRemarkDO> pageResult = userCreativeRemarkService.getUserCreativeRemarkPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserCreativeRemarkRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出创意文案关联评论 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-creative-remark:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserCreativeRemarkExcel(@Valid UserCreativeRemarkPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserCreativeRemarkDO> list = userCreativeRemarkService.getUserCreativeRemarkPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "创意文案关联评论.xls", "数据", UserCreativeRemarkRespVO.class,
                        BeanUtils.toBean(list, UserCreativeRemarkRespVO.class));
    }

}